package com.kylin.module.services.actions;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.services.business.ServiceBlogic;
import com.kylin.module.services.forms.ServiceForm;
import com.kylin.module.services.orm.Service;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

/**
 * 服务模块控制器
 * 完成系统中服务栏目的增加 查看 删除等功能
 * @author duhui
 *
 */
public class ServiceAction extends BaseDispatchAction implements
		BaseActionInterface {

	protected static Logger log = Logger.getLogger(ServiceAction.class);

	/*  
	 * 创建新的服务记录 的业务功能  
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward add(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("开始保存服务信息 ");
		ServiceForm form = (ServiceForm) actionForm;
		String forward = "addSucc";
		form.setLanguage(getSessionLocale(request).toString());
		try {
			ServiceBlogic blogic = new ServiceBlogic();
			blogic.addService(form);
			request.setAttribute(Constants.MSGKEY, new MsgKey(
					"msg.addService.succ", "登记服务申请成功"));
		} catch (Exception e) {
			doException(request, e, forward);
			doException(request, e, "reAdd");
		}
		return mapping.findForward(forward);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("删除服务 : " + request.getParameter("id"));
		String id = request.getParameter("id");
		try {
			ServiceBlogic blogic = new ServiceBlogic();
			blogic.deleteServiceById(new Long(id));
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
		return search(mapping, actionForm, request, response);
	}

	public ActionForward search(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {

		log.debug("查询服务列表 : " + request.getParameter("id"));
		String forward = "list";
		int pageSize = 5; // 分页列表的每页行数  
		int pageNum = 1;  // 当前页号
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;

		try {
			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {

				/** *******构造查询条件********** */
				List  conditions = new LinkedList ();
				String country = request.getParameter("country");
				if (country != null && !country.equals("")) { 
					PropertyCondition cond = new PropertyCondition("country",
							country, PropertyCondition.OPERATOR_EQ);
					conditions.add(cond);
				}

				String catServiceId = request.getParameter("catService");
				if (catServiceId != null && !catServiceId.equals("")) { 
					Dict catServiceObj = new Dict(new Long(catServiceId));
					PropertyCondition cond = new PropertyCondition(
							"catService", catServiceObj,
							PropertyCondition.OPERATOR_EQ);
					conditions.add(cond);
				}

				String language = request.getParameter("language");
				if (language != null && !language.equals("")) {
					PropertyCondition cond = new PropertyCondition("language",
							language, PropertyCondition.OPERATOR_EQ);
					conditions.add(cond);
				}

				String keyword = request.getParameter("keyword");
				if (keyword != null && !keyword.equals("")) {
					PropertyCondition cond = new PropertyCondition("content",
							"%" + keyword + "%",
							PropertyCondition.OPERATOR_LIKE);
					conditions.add(cond);
				}

				cinfo = new CriteriaInfo(Service.class, conditions);
				cinfo.setOrderby(Order.desc("createdon"));
				addUserCacheCriteriaInfo(request, cinfo);
			} else {
				cinfo = getUserCacheCriteriaInfo(request, searchId);
			}
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum, cinfo);;
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward detailService(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		log.debug("查看一条service详情  : " + request.getParameter("Id"));
		String forward = "detail";
		try {
			ServiceBlogic blogic = new ServiceBlogic();
			Service obj = blogic.getServiceById(new Long(request
					.getParameter("Id")));
			request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);
		} catch (Exception e) {
			doException(request, e, forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward update(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		return null;
	}

}
